using System.Collections.Generic;
using UnityEngine;
using StbImageSharp;
using Reinterpret.Net;
namespace Gltf
{
    public struct ImageReaderResult
    {

        /**
         * @brief The {@link ImageCesium} that was read.
         *
         * This will be `std::nullopt` if the image could not be read.
         */
        public ImageCesium image;

        /**
         * @brief Error messages that occurred while trying to read the image.
         */
        public List<string> errors;

        /**
         * @brief Warning messages that occurred while reading the image.
         */
        public List<string> warnings;
    }
    public class GltfReader
    {

        /**
           * @brief Gets the context used to control how extensions are loaded from glTF
           * files.
           */
        //   CesiumJsonReader::ExtensionReaderContext& getExtensions();

        //   /**
        //    * @brief Gets the context used to control how extensions are loaded from glTF
        //    * files.
        //    */
        //   const CesiumJsonReader::ExtensionReaderContext& getExtensions() const;

        //   /**
        //    * @brief Reads a glTF or binary glTF (GLB) from a buffer.
        //    *
        //    * @param data The buffer from which to read the glTF.
        //    * @param options Options for how to read the glTF.
        //    * @return The result of reading the glTF.
        //    */
        //   ModelReaderResult readModel(
        //       const gsl::span<const std::byte>& data,
        //       const ReadModelOptions& options = ReadModelOptions()) const;

        /**
         * @brief Reads an image from a buffer.
         *
         * The [stb_image](https://github.com/nothings/stb) library is used to decode
         * images in `JPG`, `PNG`, `TGA`, `BMP`, `PSD`, `GIF`, `HDR`, or `PIC` format.
         *
         * @param data The buffer from which to read the image.
         * @return The result of reading the image.
         */
        public static ImageReaderResult readImage(byte[] data)
        {
            Debug.Log("CesiumGltf::readImage");
            ImageReaderResult result = new ImageReaderResult();
            var image = new ImageCesium();
            result.image = image;
            image.bytesPerChannel = 1;
            image.channels = 4;

            ImageResult pImage = ImageResult.FromMemory(data, (ColorComponents)image.channels);
            if (pImage != null)
            {
                image.width = pImage.Width;
                image.height = pImage.Height;
                Debug.Log("copy image " + image.width + " x" + image.height + " x" + image.channels + "x" + image.bytesPerChannel);
                // int lastByte = image.width * image.height * image.channels * image.bytesPerChannel;
                image.pixelData = pImage.Data;
            }
            else
            {
                result.image = null;
                result.errors = new List<string>();
                // result.errors=new List<string>{pImage.SourceComp}
            }
            return result;
        }

    }
}